<template>
  <div class="vocab-test-container">
    <!-- 背景装饰（增加左侧装饰，平衡视觉） -->
    <div class="background-decoration"/>

    <!-- 主卡片（移除max-width后） -->
    <div class="test-card" :class="{ 'card-loaded': isLoaded }">
      <!-- 内部内容容器（新增，用于约束内容宽度并居中） -->
      <div class="card-content">
        <!-- 返回按钮 -->
        <button class="back-btn" @click="goBack">
          <i class="icon-arrow-left"/> 返回主页
        </button>

        <!-- 标题区域 -->
        <div class="title-section">
          <div class="title-icon">📚</div>
          <h1 class="main-title">词汇量测试</h1>
          <p class="subtitle">评估你的英语词汇掌握水平</p>
        </div>

        <!-- 基本信息 -->
        <div class="info-section">
          <div class="time-indicator">
            <i class="icon-clock"/>
            <span>时间：最快约 3 分钟</span>
          </div>
          <div class="question-count">
            <i class="icon-question"/>
            <span>题量：系统动态调整</span>
          </div>
        </div>

        <!-- 测试规则 -->
        <div class="rules-section">
          <h2 class="section-title">测试规则</h2>
          <ul class="rules-list">
            <li class="rule-item">
              <i class="icon-check"/>
              <span>测试过程中，若遇不认识的单词，请选择「不认识」选项，以降低测评偏差</span>
            </li>
            <li class="rule-item">
              <i class="icon-check"/>
              <span>系统展示的单词释义仅供参考，若存在异议，请选择最接近的答案</span>
            </li>
            <li class="rule-item">
              <i class="icon-check"/>
              <span>本系统测评标准较为严格，英语初学者建议积累一定词汇量后再进行测试</span>
            </li>
            <li class="rule-item">
              <i class="icon-check"/>
              <span>测试完成后，系统将提供详细的等级分析及学习建议，供用户参考</span>
            </li>
            <li class="rule-item">
              <i class="icon-check"/>
              <span>词汇量掌握越丰富，测试时长可能相应增加，属正常现象</span>
            </li>
          </ul>
        </div>

        <!-- 等级预览（恢复第一版卡片样式） -->
        <div class="levels-section">
          <h2 class="section-title">等级说明</h2>

          <!-- A级分组 -->
          <div class="level-group">
            <h3 class="group-title">A级（入门）</h3>
            <div class="level-cards">
              <div
                v-for="level in levels.filter(l => l.group === 'A')"
                :key="level.id"
                class="level-card"
                @mouseenter="level.hover = true"
                @mouseleave="level.hover = false"
              >
                <div class="level-badge" :style="{ backgroundColor: level.color }">{{ level.name }}</div>
                <div class="level-name">{{ level.title }}</div>
                <div class="level-range">{{ level.range }}</div>
              </div>
            </div>
          </div>

          <!-- B级分组 -->
          <div class="level-group">
            <h3 class="group-title">B级（初级）</h3>
            <div class="level-cards">
              <div
                v-for="level in levels.filter(l => l.group === 'B')"
                :key="level.id"
                class="level-card"
                @mouseenter="level.hover = true"
                @mouseleave="level.hover = false"
              >
                <div class="level-badge" :style="{ backgroundColor: level.color }">{{ level.name }}</div>
                <div class="level-name">{{ level.title }}</div>
                <div class="level-range">{{ level.range }}</div>
              </div>
            </div>
          </div>

          <!-- C级分组 -->
          <div class="level-group">
            <h3 class="group-title">C级（中级）</h3>
            <div class="level-cards">
              <div
                v-for="level in levels.filter(l => l.group === 'C')"
                :key="level.id"
                class="level-card"
                @mouseenter="level.hover = true"
                @mouseleave="level.hover = false"
              >
                <div class="level-badge" :style="{ backgroundColor: level.color }">{{ level.name }}</div>
                <div class="level-name">{{ level.title }}</div>
                <div class="level-range">{{ level.range }}</div>
              </div>
            </div>
          </div>

          <!-- D级分组 -->
          <div class="level-group">
            <h3 class="group-title">D级（高级）</h3>
            <div class="level-cards">
              <div
                v-for="level in levels.filter(l => l.group === 'D')"
                :key="level.id"
                class="level-card"
                @mouseenter="level.hover = true"
                @mouseleave="level.hover = false"
              >
                <div class="level-badge" :style="{ backgroundColor: level.color }">{{ level.name }}</div>
                <div class="level-name">{{ level.title }}</div>
                <div class="level-range">{{ level.range }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 统计与历史 -->
        <div class="stats-section">
          <div v-if="historyData" class="stat-item users-count">
            <i class="icon-users"/>
            <span>已有 {{ historyData.testPepNum }} 人完成测试</span>
            <span>累计测试次数 {{ historyData.testCount }} 次</span>
          </div>
          <div v-if="historyData" class="stat-item last-result">
            <i class="icon-history"/>
            <span>上次成绩：{{ historyData.maxLevel }}（{{ historyData.wordMax }}词）</span>
          </div>
          <div v-if="historyData" class="stat-item last-result">
            <i class="icon-history"/>
            <span>历史最高分站内排名：{{ historyData.pepSort }}</span>
          </div>
          <div v-else class="stat-item guest-hint">
            <i class="icon-save"/>
            <span>完成测试后可保存结果到个人档案</span>
          </div>
        </div>

        <!-- 开始测试按钮 -->
        <button
          class="start-btn"
          :class="{ 'btn-hover': isBtnHover }"
          @click="startTest"
          @mouseenter="isBtnHover = true"
          @mouseleave="isBtnHover = false"
        >
          开始测试
          <i class="icon-arrow-right"/>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { vocabTestRecordMsg } from '@/api/vocabTestRecord'

export default {
  data() {
    return {
      isLoaded: false,
      isBtnHover: false,
      userCount: 12384,
      lastTestResult: {
        level: 'B1',
        words: 4500
      },
      // 等级数据（A/B/C三大类分组，每类分0/1/2三级）
      levels: [],
      historyData: {}
    }
  },
  computed: {
    formattedUserCount() {
      return this.userCount.toLocaleString()
    }
  },
  mounted() {
    // 加载历史数据
    this.loadHistoryData()
    // 加载等级
    this.initLevelsData()
    // 页面加载动画
    setTimeout(() => {
      this.isLoaded = true
    }, 300)
  },
  methods: {
    goBack() {
      // 返回主页逻辑
      this.$router.push('/')
    },
    startTest() {
      // 开始测试逻辑
      this.$router.push('/vocabTestStart')
    },
    async loadHistoryData() {
      this.historyData = await vocabTestRecordMsg()
      if (this.historyData) {
        this.historyData.testPepNum = Number(this.historyData.testPepNum).toLocaleString()// 默认按本地格式加千分符
      }
    },
    initLevelsData() {
      this.levels = [
        // A级（入门）
        {
          id: 11,
          group: 'A',
          name: 'A0',
          title: '入门',
          range: '0–600词',
          color: '#94A3B8',
          hover: false
        },
        {
          id: 12,
          group: 'A',
          name: 'A1',
          title: '入门初级',
          range: '600–1400词',
          color: '#64748B',
          hover: false
        },
        {
          id: 13,
          group: 'A',
          name: 'A2',
          title: '入门进阶',
          range: '1400–2200词',
          color: '#475569',
          hover: false
        },
        {
          id: 14,
          group: 'A',
          name: 'A3',
          title: '入门精通',
          range: '2200–3000词',
          color: '#384659',
          hover: false
        },
        // B级（初级）
        {
          id: 21,
          group: 'B',
          name: 'B0',
          title: '初级入门',
          range: '3000–3500词',
          color: '#70a1f3',
          hover: false
        },
        {
          id: 22,
          group: 'B',
          name: 'B1',
          title: '初级',
          range: '3500–4100词',
          color: '#4176ea',
          hover: false
        },
        {
          id: 23,
          group: 'B',
          name: 'B2',
          title: '初级进阶',
          range: '4100–4800词',
          color: '#3564ea',
          hover: false
        },
        {
          id: 24,
          group: 'B',
          name: 'B3',
          title: '初级精通',
          range: '4800–5500词',
          color: '#0739cc',
          hover: false
        },
        // C级（中级）
        {
          id: 7,
          group: 'C',
          name: 'C0',
          title: '中级入门',
          range: '5500–7500词',
          color: '#F97316',
          hover: false
        },
        {
          id: 8,
          group: 'C',
          name: 'C1',
          title: '中级',
          range: '7500–9000词',
          color: '#ed5f16',
          hover: false
        },
        {
          id: 9,
          group: 'C',
          name: 'C2',
          title: '中级进阶',
          range: '9000-10000词',
          color: '#ea5721',
          hover: false
        },
        {
          id: 91,
          group: 'C',
          name: 'C3',
          title: '中级精通',
          range: '10000-12000词',
          color: '#af460f',
          hover: false
        },
        // D级（高级）
        {
          id: 10,
          group: 'D',
          name: 'D0',
          title: '高级入门',
          range: '12000–13500词',
          color: '#F97316',
          hover: false
        },
        {
          id: 11,
          group: 'D',
          name: 'D1',
          title: '高级',
          range: '13500–15000词',
          color: '#EA580C',
          hover: false
        },
        {
          id: 12,
          group: 'D',
          name: 'D2',
          title: '高级进阶',
          range: '15000-17500词',
          color: '#DC2626',
          hover: false
        },
        {
          id: 13,
          group: 'D',
          name: 'D3',
          title: '高级精通',
          range: '17500+词',
          color: '#a80808',
          hover: false
        }
      ]
    }
  }
}
</script>

<style scoped>
/* 基础样式保持不变 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Noto Sans SC', 'Inter', sans-serif;
  user-select: none;
}

.vocab-test-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

/* 3. 背景装饰优化（增加左侧元素，平衡视觉） */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #E6F0FF 0%, #FFFFFF 100%);
  z-index: 1;
}

/* 左侧增加装饰圆，与右侧平衡 */
.background-decoration::before {
  content: '';
  position: absolute;
  top: 10%;
  right: 10%;
  width: 200px;
  height: 200px;
  background: rgba(59, 130, 246, 0.05);
  border-radius: 50%;
}

.background-decoration::after {
  content: '';
  position: absolute;
  bottom: 10%;
  left: 10%; /* 左侧底部增加装饰 */
  width: 150px;
  height: 150px;
  background: rgba(16, 185, 129, 0.05);
  border-radius: 50%;
}

.test-card {
  width: 100%;
  /* 移除max-width，让卡片占满宽度 */
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  /* 左右内边距自适应：小屏15px，大屏自动增加 */
  padding: 30px clamp(15px, 8vw, 100px);
  position: relative;
  z-index: 2;
  opacity: 0;
  transform: translateY(20px);
  transition: all 0.5s ease-out;
}

/* 新增内部内容容器（约束核心内容宽度并居中） */
.card-content {
  max-width: 550px; /* 保持原核心内容的最大宽度 */
  margin: 0 auto; /* 让内容在卡片中居中 */
  width: 100%; /* 确保小屏时占满宽度 */
}

.card-loaded {
  opacity: 1;
  transform: translateY(0);
}

.back-btn {
  background: transparent;
  border: none;
  color: #6B7280;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 5px 0;
  margin-bottom: 15px;
  transition: color 0.2s;
}

.back-btn:hover {
  color: #3B82F6;
}

.icon-arrow-left::before {
  content: '←';
}

.title-section {
  text-align: center;
  margin-bottom: 30px;
}

.title-icon {
  font-size: 40px;
  margin-bottom: 15px;
}

.main-title {
  font-size: 28px;
  font-weight: 700;
  color: #1F2937;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 16px;
  color: #6B7280;
}

.info-section {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 30px;
  padding: 15px;
  background: #F9FAFB;
  border-radius: 12px;
}

.time-indicator, .question-count {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #4B5563;
  font-size: 14px;
}

.icon-clock::before {
  content: '🕒';
}

.icon-question::before {
  content: '❓';
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #1F2937;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-title::before {
  content: '';
  width: 4px;
  height: 18px;
  background: #3B82F6;
  border-radius: 2px;
}

.rules-section {
  margin-bottom: 30px;
}

.rules-list {
  list-style: none;
}

.rule-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 12px;
  color: #4B5563;
  font-size: 15px;
}

.rule-item:last-child {
  margin-bottom: 0;
}

.icon-check::before {
  content: '✅';
  flex-shrink: 0;
  margin-top: 1px;
}

/* 等级说明（恢复第一版卡片样式） */
.levels-section {
  margin-bottom: 30px;
}

/* 等级分组 */
.level-group {
  margin-bottom: 20px;
}

.group-title {
  font-size: 16px;
  color: #1F2937;
  margin-bottom: 12px;
  padding-left: 4px;
  font-weight: 600;
}

/* 卡片容器 */
.level-cards {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 8px;
  scrollbar-width: thin;
}

.level-cards::-webkit-scrollbar {
  height: 6px;
}

.level-cards::-webkit-scrollbar-thumb {
  background: #E5E7EB;
  border-radius: 3px;
}

/* 单个等级卡片（保持第一版样式） */
.level-card {
  min-width: 110px;
  padding: 18px 15px;
  background: #F9FAFB;
  border-radius: 12px;
  text-align: center;
  transition: transform 0.2s, box-shadow 0.2s;
  flex-shrink: 0;
}

.level-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.06);
}

.level-badge {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  margin: 0 auto 12px;
}

.level-name {
  font-size: 15px;
  font-weight: 600;
  color: #1F2937;
  margin-bottom: 6px;
}

.level-range {
  font-size: 13px;
  color: #6B7280;
}

.stats-section {
  margin-bottom: 35px;
  padding: 15px;
  background: #F9FAFB;
  border-radius: 12px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  color: #4B5563;
  font-size: 14px;
}

.stat-item:last-child {
  margin-bottom: 0;
}

.icon-users::before {
  content: '👥';
}

.icon-history::before {
  content: '📊';
}

.icon-save::before {
  content: '💾';
}

.start-btn {
  width: 100%;
  padding: 16px;
  background: #3B82F6;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.start-btn::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: all 0.6s ease;
}

.start-btn.btn-hover {
  background: #2563EB;
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(59, 130, 246, 0.2);
}

.start-btn.btn-hover::after {
  left: 100%;
}

.icon-arrow-right::before {
  content: '→';
}

@media (max-width: 480px) {
  .test-card {
    padding: 20px 15px; /* 小屏紧凑布局 */
    border-radius: 12px; /* 小屏圆角略小 */
  }

  .main-title {
    font-size: 24px;
  }

  .info-section {
    flex-direction: column;
    gap: 10px;
    align-items: center;
  }

  .level-card {
    min-width: 95px;
    padding: 15px 10px;
  }

  .level-badge {
    width: 36px;
    height: 36px;
    font-size: 14px;
  }

  .level-name {
    font-size: 14px;
  }

  .level-range {
    font-size: 12px;
  }

  .start-btn {
    font-size: 16px;
    padding: 14px;
  }
}
</style>
